package homework;

public class HomeWork23 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode cur1=headA;
        ListNode cur2=headB;
        int count1=0;
        while (cur1!=null){
            cur1=cur1.next;
            count1++;
        }
        int count2=0;
        while (cur2!=null){
            cur2=cur2.next;
            count2++;
        }
        int len=0;
        cur1=headA;
        cur2=headB;
        if(count1>count2){
            len=count1-count2;
            for (int i = 0; i < len; i++) {
                cur1=cur1.next;
            }
            while (cur1!=null){
                if(cur1==cur2){
                    return cur1;
                }
                cur1=cur1.next;
                cur2=cur2.next;
            }
            return null;
        }else{
            len=count2-count1;
            for (int i = 0; i < len; i++) {
                cur2=cur2.next;
            }
            while (cur1!=null){
                if(cur1==cur2){
                    return cur1;
                }
                cur1=cur1.next;
                cur2=cur2.next;
            }
            return null;
        }

    }
}
